package com.czmh.etc.onlinepubsys.services.impl;

import com.czmh.etc.onlinepubsys.maper.AppVersionMapper;
import com.czmh.etc.onlinepubsys.model.AppVersion;
import com.czmh.etc.onlinepubsys.services.AppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("appVersionService")
public class AppVersionServiceImpl implements AppVersionService {
	@Autowired
	private AppVersionMapper appVersionMapper;
	
	@Override
	public AppVersion queryObject(String id){
		return appVersionMapper.queryObject(id);
	}
	
	@Override
	public List<AppVersion> queryList(Map<String, Object> map){
		return appVersionMapper.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return appVersionMapper.queryTotal(map);
	}
	
	@Override
	public int save(AppVersion appVersion){
		return appVersionMapper.save(appVersion);
	}

    @Override
    public int saveSelective(AppVersion appVersion){
        return appVersionMapper.saveSelective(appVersion);
    }

    @Override
    public int saveList(List<AppVersion> appVersions){
        return appVersionMapper.saveList(appVersions);
    }

	@Override
	public int update(AppVersion appVersion){
		return appVersionMapper.update(appVersion);
	}
	
	@Override
	public int delete(String id){
		return appVersionMapper.delete(id);
	}
	
	@Override
	public int deleteBatch(String[] ids){
		return appVersionMapper.deleteBatch(ids);
	}

	@Override
	public List<AppVersion> getAppVersionsByType(int type) {
		return appVersionMapper.getAppVersionsByType(type);
	}

	/**
	 * 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0
	 * @param version1
	 * @param version2
	 * @return
	 */
	@Override
	public int compareVersion(String version1, String version2) throws Exception {
		if (version1 == null || version2 == null) {
			throw new Exception("compareVersion error:illegal params.");
		}
		//注意此处为正则匹配，不能用"."；
		String[] versionArray1 = version1.split("\\.");
		String[] versionArray2 = version2.split("\\.");
		int idx = 0;
		//取最小长度值
		int minLength = Math.min(versionArray1.length, versionArray2.length);
		int diff = 0;
		while (idx < minLength
				&& (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0//先比较长度
				&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {//再比较字符
			++idx;
		}
		//如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；
		diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
		return diff;
	}
	
}
